package com.chunfeng.mypen.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chunfeng.mypen.dao.RouterDao;
import com.chunfeng.mypen.entity.Router;
import com.chunfeng.mypen.properties.SystemProperties;
import com.chunfeng.mypen.result.JsonRequest;
import com.chunfeng.mypen.result.exenum.RequestException;
import com.chunfeng.mypen.service.RouterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 路由业务层
 *
 * @author by 春风能解释
 * <p>
 * 2023/11/18
 */
@Service
public class RouterServiceImpl extends ServiceImpl<RouterDao, Router> implements RouterService {

    /**
     * 路由Dao注入
     */
    @Autowired
    private RouterDao routerDao;

    /**
     * 系统配置
     */
    @Autowired
    private SystemProperties systemProperties;

    /**
     * 条件查询路由
     *
     * @param current 当前页码
     * @param size    每页长度
     * @param router  条件
     * @return JSON
     */
    @Override
    public JsonRequest<List<Router>> lookRouter(Integer current, Integer size, Router router) {
        Page<Router> routerPage = routerDao.selectRouter(new Page<>(current, size), router);
        //拿到列表值
        List<Router> routers = routerPage.getRecords();
        if (routers.isEmpty()) {
            return JsonRequest.error(RequestException.NOT_FOUND);
        }
        Map<String, Object> page = new HashMap<>();
        page.put(systemProperties.getPageKeyName(), routerPage.getTotal());
        return JsonRequest.success(routers, page);
    }

    /**
     * 添加一条路由信息
     *
     * @param router 路由信息
     * @return JSON
     */
    @Override
    public JsonRequest<Integer> insertOneRouter(Router router) {
        Router router1 = routerDao.selectOne(new LambdaQueryWrapper<Router>()
                .eq(Router::getUri, router.getUri())//URI
                .eq(Router::getMethod, router.getMethod())//请求方法
                .eq(Router::getLocation, router.getLocation()));//后端类型
        //URI与method能够确定一个唯一的[后端/前端]路由接口，如果输入的出现了重复，则判定已有该路由
        if (Objects.nonNull(router1)) {
            return JsonRequest.error(RequestException.INSERT_ERROR);
        }
        int column = routerDao.insert(router);
        //插入失败
        if (column < 1) {
            return JsonRequest.error(RequestException.INSERT_ERROR);
        }
        return JsonRequest.success(column);
    }

    /**
     * 修改一条路由信息
     *
     * @param router 待修改的路由信息
     * @return JSON
     */
    @Override
    public JsonRequest<Integer> updateOneRouter(Router router) {
        Router router1 = routerDao.selectById(router.getId());
        if (Objects.isNull(router1)) {
            return JsonRequest.error(RequestException.NOT_FOUND);
        }
        int column = routerDao.updateById(router);
        if (column < 1) {
            return JsonRequest.error(RequestException.UPDATE_ERROR);
        }
        return JsonRequest.success(column);
    }

    /**
     * 批量删除路由
     *
     * @param ids 路由ID
     * @return JSON
     */
    @Override
    public JsonRequest<Integer> deleteRouter(List<String> ids) {
        Integer size = routerDao.selectCountRouter(ids);
        if (size != ids.size()) {
            return JsonRequest.error(RequestException.NOT_FOUND);
        }
        int column = routerDao.deleteBatchIds(ids);
        if (column < 1) {
            return JsonRequest.error(RequestException.DELETE_ERROR);
        }
        return JsonRequest.success(column);
    }
}
